package com.example.springbootdemo.controller;

import com.example.springbootdemo.entity.EsContent;
import com.example.springbootdemo.service.EsService;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: EsController
 * @Description:
 * @Author: TXW
 * @Date: 2024-02-02
 */

@RestController
@RequestMapping("esSearch")
public class EsController {

    @Resource
    private EsService esService;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return esService.createIndex();
    }

    @GetMapping("/getIndex")
    public GetIndexResponse getIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return esService.getIndex();
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 2.初始化-> 新增数据
        EsContent document = new EsContent();
        document.setTitle("老汉");
        document.setContent("推*技术过硬");
        return esService.insert(document);
    }

    @GetMapping("/search/{name}")
    public List<EsContent> search(@PathVariable String name) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<EsContent> wrapper = new LambdaEsQueryWrapper<>();
//        wrapper.eq(EsContent::getTitle, name)
//                .or()
//                .eq(EsContent::getCheckWords,name);

        wrapper.like(EsContent::getTitle, name);
        EsPageInfo<EsContent> documentPageInfo = esService.pageQuery(wrapper,1,10);
        return esService.selectList(wrapper);

    }


    @GetMapping("/searchPage/{name}")
    public EsPageInfo<EsContent> searchPage(@PathVariable String name) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<EsContent> wrapper = new LambdaEsQueryWrapper<>();
//        wrapper.eq(EsContent::getTitle, name)
//                .or()
//                .eq(EsContent::getCheckWords,name);

        wrapper.like(EsContent::getTitle, name);
        EsPageInfo<EsContent> documentPageInfo = esService.pageQuery(wrapper,1,10);
        return documentPageInfo;

    }

}
